using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using MyUtils;

namespace Test.MyUtils.WeakDelegate
{
	public class WeakToSimpleSubscriber<T> : EventSubscriber<T> where T : EventPublisher
	{
		readonly SimpleEventPublisher _publisher;

		public WeakToSimpleSubscriber(SimpleEventPublisher publisher)
		{
			_publisher = publisher;
		}

		Action _actionHandler;

		public override void Subscribe()
		{
			// save weak handler for manual unsubscribe ability
			_actionHandler = (Action)Weak.MagicSubscribe(() => _publisher.EventSimple += Handler);
			// =>
			//var pub = _publisher;
			//Weak.Subscribe<Action>(x => pub.EventSimple += x, x => pub.EventSimple -= x, Handler);
		}

		public override void Unsubscribe()
		{
			_publisher.EventSimple -= _actionHandler;
		}

		void Handler()
		{
			InvokationCount++;
		}

	}
}